#include <dp8051xp.h>
#include <stdio.h>
#include <intrins.h>
#include "sys.h"
#include "pmu_cec.h"
#include "uart.h"
//#define  DEBUG_PRINTF 

/*
 static struct pmu_cec g_pmu_cec =
{
	// Check receive User Control Pressed> ["Power On Function"] message length = 0x02
    .cec_wake_up_fn = {{0x00,0x44,0x6D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
				 	   {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
				 	   {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}},
    // Check receive <Give Device Power Status> ? 0x8F message length = 0x01
    .cec_rx_fn = {0x00,0x8F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
    // Reply <Report Power Status> ["Standby"] 
    .cec_tx_fn = {0x00,0x90,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
   // .cec_eom   = {0x04,0x00}, 
    .logic_addr=  0x03, //Read OPT1 to get CEC LA
    .cec_div   =  0x31,
    .cec_freq  =  0x1a,
    .pmu_clk   =  0x1b,
}
*/

static UINT8 g_cec_wk = 0;

 UINT8  g_cec_wake_up_fn[CEC_WKUP_GRP_ZIZE][CEC_CMD_SZIE_BYTE] =
{    
	{0x00,0x44,0x6D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
};	

 void pmu_cec_init()
{
    #ifdef PMU_CEC    
    UINT8 i,j;
    UINT8 log_addr;   

    /*enable cec pin */
#if  PMU_MCU_M3503
    CEC_PIN_EN(XPMU_CEC);
#else
    CEC_PIN_EN(XPMU1_CEC);
#endif
    #ifdef DEBUG_PRINTF 
    // printf("\n cec_in_eanble=%0bx\n",READ_BYTE(SYS_REG_IER));
    #endif

    /*set cec logic addr*/
    CEC_SET_LOGADD(LOGIC_ADDR);
    #ifdef DEBUG_PRINTF 
    printf("cec_set_logaddr=%0bx\n",READ_BYTE(LOGIC_ADDR_REG));
    #endif
    /*set cec div*/
    CEC_SET_DIV(CEC_DIV);
    #ifdef DEBUG_PRINTF 
    printf("cec_set_iv=%0bx\n",READ_BYTE(DIVIDER_REG));
    #endif
    /*set cec frq */
    CEC_SET_FRQ(CEC_FREQ);
    #ifdef DEBUG_PRINTF 
    printf("cec_set_freq=%0bx\n",READ_BYTE(FRQ_RES_REG));
    #endif
    /* set logic address*/
     log_addr =CEC_GET_LOGADDR();
     #ifdef DEBUG_PRINTF 
     printf("cec_logic_addr=%0bx\n",CEC_GET_LOGADDR());
     #endif
	 /*set wakeup cmd*/
     for(j=0;j<CEC_WKUP_GRP_ZIZE;j++)
    {
      #ifdef DEBUG_PRINTF   
      printf("\n");  
      #endif
      for(i=0;i<CEC_CMD_SZIE_BYTE;i++)
      {       
         if((i==0)&&(j==0))//only config first grp
            g_cec_wake_up_fn[j][i] = ((g_cec_wake_up_fn[j][i])&0xf0)|(log_addr&0x0f);
         
         PMU_WRITE_BYTE((CEC_WKUP_CMD1_REG+CEC_CMD_SZIE_BYTE*j+i),g_cec_wake_up_fn[j][i]);
         
         #ifdef DEBUG_PRINTF 
         printf("grp=%0bx,index=%0bx,cmd reg =%0bx\n",j,i,READ_BYTE(CEC_WKUP_CMD1_REG+CEC_CMD_SZIE_BYTE*j+i));
         #endif
         
      }  
    }
     
     /*set cee irp*/
    CEC_SYS_INT_IPR();
    /*enable cec interrupt*/
    CEC_SYS_INT_ENABLE(); 
    #ifdef DEBUG_PRINTF  
    printf("cec interrupt reg =%0bx\n",PMU_READ_BYTE(SYS_REG_IER));
    #endif
    #endif
}

char get_cec_cmd()
{
    #ifdef PMU_CEC
    
    char ret = ERROR;
      if(g_cec_wk == 1)
        ret = SUCCESS;
    return ret;
    
    #endif
}

void  set_get_cec_cmd(char status)
{
    #ifdef PMU_CEC
    
    if(status == 1)
        g_cec_wk =1;
    else
        g_cec_wk =0;
    
    #endif
}

